package so.sao.code.service.web.portal;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import so.sao.auth.autoreg.annotation.ButtonResource;
import so.sao.auth.autoreg.annotation.MenuResource;
import so.sao.code.dto.request.EnterpriseAdvSetting;
import so.sao.code.dto.request.EnterpriseSetting;
import so.sao.code.dto.request.EnterpriseUrl;
import so.sao.code.service.domain.entity.EnterpriseOperationLog;
import so.sao.code.service.domain.entity.TagBatchDownloadLog;
import so.sao.code.service.infrastructure.SessionContext;
import so.sao.code.service.infrastructure.model.vo.EnterpriseInfoVO;
import so.sao.code.service.infrastructure.model.vo.TagBatchDownloadLogVO;
import so.sao.code.service.service.EnterpriseInfoService;
import so.sao.top.core.util.PageUtil;

/**
 * Created by Administrator on 2017/10/20.
 */
@Api(tags = "enterpriseInfo", description = "企业基本信息")
@RestController
@RequestMapping("portal/enterpriseInfo")
@MenuResource(perm = "enterprise.*", url = "enterprise")
public class EnterpriseInfoController {
	@Autowired
	EnterpriseInfoService enterpriseInfoService;

	@Autowired
	SessionContext context;

	@ApiOperation(value = "企业添加域名", notes = "企业添加域名")
	@ResponseBody
//	@ButtonResource(url = "addUrl")
//	@RequiresPermissions("enterprise.addUrl")
	@PostMapping(value = "addUrl")
	public ResponseEntity addUrl(@RequestBody EnterpriseUrl enterpriseUrl) {
		enterpriseInfoService.addUrl(enterpriseUrl.getUrl(), enterpriseUrl.getEseId());
		return new ResponseEntity<>(HttpStatus.OK);
	}

	@ApiOperation(value = "删除企业域名", notes = "删除企业域名")
	@ResponseBody
	@PostMapping(value = "delUrl")
//	@ButtonResource(url = "delUrl")
//	@RequiresPermissions("enterprise.delUrl")
	public ResponseEntity delUrl(@RequestBody EnterpriseUrl enterpriseUrl) {
		enterpriseInfoService.delUrl(enterpriseUrl.getUrl(), enterpriseUrl.getEseId());
		return new ResponseEntity<>(HttpStatus.OK);
	}


	@ApiOperation(value = "获取企业码管理信息", notes = "获取企业码管理信息")
	@ResponseBody
	@GetMapping
	public ResponseEntity<EnterpriseInfoVO> get(final Long eseId) {
		return new ResponseEntity<>(enterpriseInfoService.get(eseId), HttpStatus.OK);
	}

	@ApiOperation(value = "修改企业码量", notes = "修改企业码量")
	@ResponseBody
	@PostMapping("modfiyCode")
	public ResponseEntity modifyEnterpiseCode(@RequestBody EnterpriseSetting setting) throws Exception {
		enterpriseInfoService.enterpriseCodeSetting(setting);
		return new ResponseEntity<>(HttpStatus.OK);
	}


	@ApiOperation(value = "企业高级设置", notes = "企业高级设置")
	@ResponseBody
	@PostMapping("advSetting")
	@ButtonResource(url = "advSetting")
	@RequiresPermissions("enterprise.advSetting")
	public ResponseEntity advSetting(@RequestBody EnterpriseAdvSetting enterpriseAdvSetting) throws Exception {
		enterpriseInfoService.advSetting(enterpriseAdvSetting.getEseId(), enterpriseAdvSetting.isSmall(),
				enterpriseAdvSetting.isAdvEncrypt());
		return new ResponseEntity<>(HttpStatus.OK);
	}


	@ApiOperation(value = "获取子企业码管理信息列表", notes = "获取子企业码管理信息列表")
	@ResponseBody
	@ButtonResource(url = "view")
	@RequiresPermissions("enterprise.view")
	@GetMapping("list")
	public ResponseEntity<List<EnterpriseInfoVO>> getList() {
		return new ResponseEntity<>(enterpriseInfoService.list(), HttpStatus.OK);
	}

	@ApiOperation(value = "获取企业操作日志", notes = "获取企业操作日志")
	@ResponseBody
	@GetMapping("logs")
	public ResponseEntity<List<EnterpriseOperationLog>> getLogs() {
		return new ResponseEntity<>(enterpriseInfoService.getActionLog(0, 1000), HttpStatus.OK);
	}

	@ApiOperation(value = "获取企业下载日志", notes = "获取企业下载日志")
	@GetMapping("downloadLogs")
	@RequiresAuthentication
	public Page<TagBatchDownloadLogVO> getDownloadLogs(HttpServletRequest request, final Long printFactoryId,
			final String tagBatchName) {
		PageRequest pageable = PageUtil.getPageRequest(request);
		Specification<TagBatchDownloadLog> spec = new Specification<TagBatchDownloadLog>() {
			@Override
			public Predicate toPredicate(Root<TagBatchDownloadLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<Predicate>();
				predicates.add(cb.equal(root.get("eseId").as(Long.class), context.CURRECT_USER.getEseId()));
				if (printFactoryId != null) {
					predicates.add(cb.equal(root.get("printFactoryId").as(Long.class), printFactoryId));
				}
				if (tagBatchName != null) {
					predicates.add(cb.like(root.get("tagBatchName").as(String.class), "%" + tagBatchName + "%"));
				}
				Predicate[] parr = new Predicate[predicates.size()];
				return cb.and(predicates.toArray(parr));
			}
		};
		return enterpriseInfoService.getDownloadLogs(spec, pageable);
	}

	@ApiOperation(value = "数据初始化", notes = "数据初始化")
	@ResponseBody
	@GetMapping("init")
	public ResponseEntity<List<EnterpriseOperationLog>> init() {
		enterpriseInfoService.initData();
		return new ResponseEntity<>(HttpStatus.OK);
	}
}
